<%@page import="com.keta.foundation.entity.RolePermission"%>
<%@page import="com.keta.foundation.entity.Role"%>
<%@page import="com.keta.foundation.entity.Permission"%>
<%@page import="com.keta.foundation.entity.Module"%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ include file="/WEB-INF/views/include.inc.jsp"%>
<%!
	public boolean isExistPermission(Role role,Permission permission){
		if(role.getRolePermissions()!=null){
			
			for(RolePermission rolePermission : role.getRolePermissions()){
				if(rolePermission.getPermission().getId()==permission.getId()){
					return true;
				}
			}
		}else{
			return false;
		}
		
		return false;
	}
	public String module_row(Module module,Role role){
	 	StringBuilder sb = new StringBuilder();
	 	if(module.getParent()!=null){
	 		sb.append("<tr id=\"" + module.getId()+ "\" pid=\""+module.getParent().getId()+"\">");
	 	}else{
	 		sb.append("<tr id=\"" + module.getId()+ "\" >");
	 	}
	 	
	 	sb.append("<td><span class=\"button chk checkbox_false_full setAll\" />"+module.getName()+"</td>");
	 	sb.append("<td>");
	 	if(module.getPermissions()!=null){
		 	for(Permission permission : module.getPermissions()){
		 		if(isExistPermission(role,permission)){
		 			sb.append("<span class=\"pmn\">"+permission.getName()+"</span><span class=\"button chk checkbox_true_full pmnc\" />");
		 		}else{
		 			sb.append("<span class=\"pmn\">"+permission.getName()+"</span><span class=\"button chk checkbox_false_full pmnc\" />");
		 		}
		 	}
	 	}
	 	sb.append("</td>");
	 	sb.append("</tr>");
		return sb.toString();
	}
	
	public String role_tree(Module module,Role role){
		StringBuilder sb = new StringBuilder();
		sb.append(module_row(module,role));
		if(module.getChildren()!=null){
			for(Module subModule :module.getChildren()){
				sb.append(role_tree(subModule,role));
			}
		}
		return sb.toString();
	}
%>
<% 
	Module rootModule = (Module)request.getAttribute("rootModule");
	Role role = (Role)request.getAttribute("role");
	String role_tree = role_tree(rootModule,role);
	System.out.print(role_tree);
%>
<script type="text/javascript">
	
	$(document).ready(
		function() {
			//初始化treeTable
			var option = {
				theme : 'default',
				expandLevel : 3
			};
			$('.treeTable').treeTable(option);

			// 初始化全选状态
			$(".setAll").each(
				function() {
					var $td = $(this).parent().nextAll("td");
					var notPickLength = $("span[class*=checkbox_false_full]",$td).length;
					if (notPickLength == 0) {
						$(this).attr("class","button chk checkbox_true_full setAll");
					}
				});
	});

</script>
<div class="pageContent">
	<form action="#">
		<div class="pageFormContent" layoutH="56">
			<input type="hidden" name="id" value="${role.id}"/>
			<dl>
				<dt>名称：</dt>
				<dd>
					<input type="text" name="name" class="input-medium required"
						readOnly="readOnly" value="${role.name}"/>
				</dd>
			</dl>
			<dl>
				<dt>角色描述：</dt>
				<dd>
					<input type="text" style="width:400px;" name="description" class="input-medium" readOnly="readOnly"
						value="${role.description}"  />
				</dd>
			</dl>
			<div class="divider"></div>
			<table class="treeTable list" width="100%">
				<tr>
					<td>模块名称</td>
					<td width="70%">操作权限</td>
				</tr>
				
				<%=role_tree %>

			</table>
		</div>

		<div class="formBar">
			<ul>
				<li><div class="button">
						<div class="buttonContent">
							<button type="button" class="close">关闭</button>
						</div>
					</div></li>
			</ul>
		</div>
	</form>
</div>